package com.bw.lyc.network.retrofit;

import com.bw.lyc.network.Api;
import com.bw.lyc.network.http.HttpImpl;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class UploadRetrofit implements HttpImpl {

    private UploadRetrofit() {
    }

    private static Retrofit retrofit;

    @Override
    public Retrofit getRetrofit() {
        return retrofit;
    }

    public static class Build {

        public Build() {
            createRetrofit();
        }

        private void createRetrofit() {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient.Builder okBuild = new OkHttpClient.Builder();
            okBuild.addInterceptor(interceptor);
            okBuild.connectTimeout(30 * 1000, TimeUnit.MILLISECONDS);
            okBuild.writeTimeout(300 * 1000, TimeUnit.MILLISECONDS);
            okBuild.readTimeout(30 * 1000, TimeUnit.MILLISECONDS);
            Retrofit.Builder builder = new Retrofit.Builder();
            builder.client(okBuild.build());
            builder.baseUrl(Api.BaseUrl);
            builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
            builder.addConverterFactory(GsonConverterFactory.create());
            retrofit = builder.build();
        }

        public UploadRetrofit build() {
            return new UploadRetrofit();
        }
    }
}
